from pyo import *
import time
import gammeDict


class Midi():
    def __init__(self, parent):
        self.parent= parent
        self.s=Server()
        self.s.setMidiInputDevice(1)
        self.s.boot().start()
        
        ###Pour Test
        self.old_c=[60., 60., 60.]
        self.new_c=[0.0, 0.0, 0.0]
        self.old=60
        self.new=0
        ###
        
        #Joueur 1
        self.gammeP1= self.octave(gammeDict.level[gammeDict.nom[self.parent.getLevelP1()]], self.parent.getOctaveP1())
        self.oldP1=self.gammeP1[7]
        self.newP1=0
        self.notesP1=Notein(1, first=self.gammeP1[0], last=self.gammeP1[14])
        self.notesP1.setStealing(True)
        self.trigOnChangeP1 = Change(self.notesP1['pitch'])
        self.callFuncP1 = TrigFunc(self.trigOnChangeP1, function=self.getNotesP1)
        #Joueur 2
        self.gammeP2= self.octave(gammeDict.level[gammeDict.nom[self.parent.getLevelP2()]], self.parent.getOctaveP2())
        self.oldP2=self.gammeP2[7]
        self.newP2=0
        self.notesP2=Notein(1, first=self.gammeP2[0], last=self.gammeP2[14])
        self.notesP2.setStealing(True)
        self.trigOnChangeP2 = Change(self.notesP2['pitch'])
        self.callFuncP2 = TrigFunc(self.trigOnChangeP2, function=self.getNotesP2)
        
        self.pat= Pattern(self.parent.moveBall, 0.02)
    
    def go(self):
        self.pat.play()
    
    def arret(self):
        self.pat.stop()
    
    def newGamme(self, joueur):
        if joueur=="P1":
            if self.parent.getLevelP1()<len(gammeDict.nom):
                #temp= gammeDict.level[gammeDict.nom[self.parent.getLevelP1()]]
                self.gammeP1=self.octave(gammeDict.level[gammeDict.nom[self.parent.getLevelP1()]], self.parent.getOctaveP1())
            else:
                return
        elif joueur=="P2":
            if self.parent.getLevelP2()<len(gammeDict.nom):
                #temp= gammeDict.level[gammeDict.nom[self.parent.getLevelP2()]]
                self.gammeP2=self.octave(gammeDict.level[gammeDict.nom[self.parent.getLevelP2()]], self.parent.getOctaveP2())
            else:
                return
        
        ###Pour Test !
        t=temp[7]
        self.old=t
        self.new_c=[0.0, 0.0, 0.0]
        self.old_c=[float(t), float(t), float(t)]
        ###
    
    def getNotes2(self):
        gamme= gammeDict.level[gammeDict.nom[self.parent.getLevelP1()]]
        self.new_c= self.notesP1.get("pitch", True)
        if self.new_c[0]!=self.old_c[0]:
            new = self.new_c[0]
        elif self.new_c[1]!=self.old_c[1]:
            new = self.new_c[1]
        elif self.new_c[2]!=self.old_c[2]:
            new = self.new_c[2]
        self.old_c = self.new_c
        if new in gamme:
            i_c= gamme.index(self.old)
            if new == gamme[i_c-1]:
                self.old=new
                self.parent.moveP1Down()
            elif new == gamme[i_c+1]:
                self.parent.moveP1Up()
                self.old=new
        return
    
    def getNotes3(self):
        gamme= gammeDict.level[gammeDict.nom[self.parent.getLevelP1()]]
        self.new= self.notesP1.get("pitch")
        temp= self.notesP1.get("velocity")
        i_c= gamme.index(self.old)
        print self.new,temp
        if self.new==gamme[0] or self.new == gamme[i_c-1] and temp!=0:
            print "Down"
            self.old=self.new
            self.parent.moveP1Down()
        elif self.new==gamme[14] or self.new == gamme[i_c+1] and temp!=0:
            print "Up"
            self.old=self.new
            self.parent.moveP1Up()
    
    def getNotesP1(self):
        self.newP1= self.notesP1.get("pitch")
        i_c= self.gammeP1.index(self.oldP1)
        #print "newP1:", self.newP1, "oldP1:", self.oldP1, "i_c:", i_c
        if self.newP1>=self.gammeP1[0] and self.newP1 == self.gammeP1[i_c-1]:
            self.oldP1=self.newP1
            self.parent.moveP1Down()
        elif self.newP1<=self.gammeP1[14] and self.newP1 == self.gammeP1[i_c+1]:
            self.oldP1=self.newP1
            self.parent.moveP1Up()
    
    def getNotesP2(self):
        self.newP2= self.notesP2.get("pitch")
        i_c= self.gammeP2.index(self.oldP2)
        #print "newP2:", self.newP2, "oldP2:", self.oldP2, "i_c:", i_c
        if self.newP2>=self.gammeP2[0] and self.newP2 == self.gammeP2[i_c-1]:
            self.oldP2=self.newP2
            self.parent.moveP2Down()
        elif self.newP2<=self.gammeP2[14] and self.newP2 == self.gammeP2[i_c+1]:
            self.oldP2=self.newP2
            self.parent.moveP2Up()
    
    def stop(self):
        self.s.stop()
        time.sleep(.3)
    
    #Retourne la gamme octaviee a l'octave standard voulu ( C-2 a G8, C3 etant le Do central)
    def octave(self, gamme, octa):
        temp=[]
        if octa >= -1 and octa <= 7:
            for i in gamme:
                temp.append(i)
            for i in range(len(temp)):
                temp[i]=temp[i]+((octa-3)*12)
        return temp

    
    
    
    
if __name__ == '__main__': 
    m= Midi(None)
    m.s.gui(locals())